define(["jquery","Swiper","text!modules/home/home.html","modules/footer/footer","modules/header/header","text!modules/home/home.html","css!modules/swiper/swiper-3.4.0.min.css","css!modules/home/home.css"],function($,_,html,footer,header){
	function render(){
		$("body").empty();
		//插入页脚页头
		header.render();
		footer.render();
		$("body").append(html);
				// 获取数据banner
		$.ajax({
			url:"http://www.vrserver.applinzi.com/aixianfeng/apihome.php",
			type:"get",
			success:function data(res){
				var data=$.parseJSON(res);
				banner(data);
				menu(data)
			}
		});
		//扫一扫
		wx.ready(function () {
			document.querySelector(".header_scan").onclick=function(){
			wx.scanQRCode({
		    needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
		    scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
		    success: function (res) {
		    	var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
				}
			});
		}

 	});
		//获取热卖商品
		$.ajax({
			url:"http://www.vrserver.applinzi.com/aixianfeng/apihomehot.php",
			type:"get",
			success:function data(res){
				var data=$.parseJSON(res);
				homehot(data);
			}
		});
		//轮播图
		function banner(data){
			var data=data.data.slide;
			for(i=0;i<data.length;i++){
				var img=$("<div class="+"swiper-slide"+"><a href="+"#"+"><img src="+data[i].activity.img+"></a></div>");
				$(".swiper-wrapper").append(img);
			}
			//调用轮播图
			var mySwiper = new Swiper('.swiper-container', {
				autoplay: 2000,//可选选项，自动滑动
					//分页器
				pagination : '.swiper-pagination',
				paginationClickable :true,
			})	
		}
		//menu
		function menu(data){
			var data=data.data.menu;
			for(i=0;i<data.length;i++){
				var nav_icon=$("<div class="+"nav_icon"+"></div>").css('backgroundImage',"url("+data[i].activity.img+")");
				var nav_p=$("<p class="+"nav_p"+">"+data[i].activity.name+"</p>");	
				var li=$("<li class="+"nav_li"+"></li>");
				(li.append(nav_icon).append(nav_p)).appendTo($(".nav"));
			}
			//跳转到秒杀
			$(".nav_li:eq(1)").on("touchstart",function(){
				window.location.href="#seckill";
			})
		}
		//热卖商品
		function homehot(data){
			var data=data.data;
			var length=data.length;
			console.log(data[0])
			for(var i=3;i<length;i=i+3){
			var ul=crateUl();
			var	Li3=crateLi(data[i-3]);
			var	Li2=crateLi(data[i-2]);
			var	Li1=crateLi(data[i]);	
				ul.append(Li3).append(Li2).append(Li1);
				($("#home_main")).append(ul);
			}
			var i=i-3;
			var lastUl=crateUl();
			for(var k=i;k<length;k++){
				var	li=crateLi(data[k]);
				lastUl.append(li);
				lastUl.appendTo($("#home_main"));
			}
// 购物车点击事件
	// 给每个商品加入数量属性code
	for( var p in data){
		data[p].code=0;
	}
if(localStorage.getItem("commodity")){
		data=JSON.parse(localStorage.getItem("commodity"));
		console.log(data[0])
	}
var num=0;// 商品数
	if(localStorage.getItem("number")){
		num=localStorage.getItem("number");
	}	
	var add=$(".home_add").get();
	for(var i=0;i<add.length;i++){
		add[i].index=i;
		add[i].onclick=function(){
			num++;		
			data[this.index].code=data[this.index].code+1;
			console.log(data[this.index].code)			
			$(".footer .number").html(num);
			localStorage.setItem("number",num);
			localStorage.setItem("commodity",JSON.stringify(data));
		}	
	}		
}
		function crateLi(data){
			 return $("<li><p class="+"home_product-name"+">"+data.name+"</p><p class="+"home_productP1"+"><span class="+"choiceness"+">精选</span><span class="+"home_give"+">"+data.pm_desc+"</span></p><p class="+"weight"+">"+data.specifics+"</p><div class="+"home_btn"+"><p class="+"productP2"+"><span class="+"Nprice"+">"+data.price+"</span><span class="+"Oprice"+">"+data.market_price+"</span></p><div class="+"home_add"+"><span></span></div></div></li>").prepend($("<div class="+"liImg"+"></div>").css("backgroundImage","url("+data.img+")"));
		}
		function crateUl(){
			return $("<ul><div class="+"home_top"+"><div><span class="+"Square"+"></span><span class="+"title"+">优秀水果</span></div><a href="+"#"+">更多</a></div><div class="+"mainImg"+"><img src="+"img/main.jpg"+"></div></ul>");
		}
}
	
	return {
		render:render
	}
	
});